package com.leoren.run.activity;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;

import com.leoren.run.R;
import com.leoren.run.activity.main.AnnouncementFragment;
import com.leoren.run.activity.main.MyFragment;
import com.leoren.run.activity.main.RunFragment;
import com.leoren.run.activity.main.ShareFragment;

/**
 * 主界面  四个Fragment
 */
public class MainFunctionActivity extends AppCompatActivity implements View.OnClickListener {

    private Fragment runFragment;
    private Fragment announceFragment;
    private Fragment shareFragment;
    private Fragment myFragment;

    private FragmentManager manager;
    private FragmentTransaction transaction;

    private BottomNavigationView mainNavView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_function);

        initLayout();

        setSelectView(0);
    }

    //初始化界面
    private void initLayout(){
        manager = getSupportFragmentManager();
        mainNavView = findViewById(R.id.main_nav_view);
        mainNavView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.main_navigation_run:
                        setSelectView(0);
                        return true;
                    case R.id.main_navigation_announce:
                        setSelectView(1);
                        return true;
                    case R.id.main_navigation_share:
                        setSelectView(2);
                        return true;
                    case R.id.main_navigation_person:
                        setSelectView(3);
                        return true;
                }
                return false;
            }
        });


    }

    //设置选中事件
    private void setSelectView(int position){
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (position){
            case 0:
                if(runFragment == null){
                    runFragment = new RunFragment();
                    transaction.replace(R.id.main_content, runFragment);
                }else {
                    transaction.show(runFragment);
                }
                break;
            case 1:
                if(announceFragment == null){
                    announceFragment = new AnnouncementFragment();
                    transaction.add(R.id.main_content, announceFragment);
                }else {
                    transaction.show(announceFragment);
                    announceFragment.onResume();
                }
                break;
            case 2:
                if(shareFragment == null){
                    shareFragment = new ShareFragment();
                    transaction.add(R.id.main_content, shareFragment);
                }else {
                    transaction.show(shareFragment);
                }
                break;
            case 3:
                if(myFragment == null){
                    myFragment = new MyFragment();
                    transaction.add(R.id.main_content, myFragment);
                }else {
                    transaction.show(myFragment);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        if(runFragment != null){
            transaction.hide(runFragment);
        }
        if(announceFragment != null){
            transaction.hide(announceFragment);
        }
        if(shareFragment != null){
            transaction.hide(shareFragment);
        }
        if(myFragment != null){
            transaction.hide(myFragment);
        }
    }

    @Override
    public void onClick(View v) {

    }
}
